﻿Imports CapaLogica
Imports CapaLogica.CL.Carrera
Imports CapaLogica.CL.UtilidadesGenerales
Imports CapaLogica.CL.Grupo

Public Class frmCargaMasivaCurso

    Private Sub tsbCargarArchivo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbCargarArchivo.Click
        Try
            If txtPath.Text.Length Then
                Dim MyConnection As System.Data.OleDb.OleDbConnection
                Dim DtSet As System.Data.DataSet
                Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
                Dim source As String = txtPath.Text

                MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + source + "';Extended Properties=Excel 8.0;")
                MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Hoja1$]", MyConnection)
                MyCommand.TableMappings.Add("Table", "Net-informations.com")
                DtSet = New System.Data.DataSet
                MyCommand.Fill(DtSet)
                dataCurso.DataSource = DtSet.Tables(0)
                MyConnection.Close()

                tsbGuardarDatos.Enabled = True
            Else
                MsgBox("Debe de seleccionar un archivo")
            End If

        Catch ex As Exception
            MsgBox("Ocurrio un error al leer el archivo")
        End Try
    End Sub

    Private Sub btn__Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_.Click
        fileExport.ShowDialog()
        txtPath.Text = fileExport.FileName
        tsbGuardarDatos.Enabled = True
    End Sub

    Private Sub tsbCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbCancelar.Click
        Me.Close()
    End Sub

    Private Sub tsbGuardarDatos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbGuardarDatos.Click
        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim rows As Integer = 0
        Dim columns As Integer = 0
        Dim listaM As List(Of List(Of String)) = New List(Of List(Of String))
        Dim listInterna As List(Of String)

            rows = dataCurso.RowCount - 2
            columns = dataCurso.ColumnCount - 1

            ''Recorre el datagrid
            For i = 0 To rows
                listInterna = New List(Of String)
                For j = 0 To columns
                    listInterna.Add(CStr(dataCurso.Item(j, i).Value))
                Next j
                listaM.Add(listInterna)
            Next i

            If GestorCurso.registrarCursoBatch(listaM) Then
                MessageBox.Show("Se cargo el archivo correctamente", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Ocurrio un error el carga", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If


    End Sub
End Class